<link href="<?php echo base_url(JS_PUBLIC);?>/fileuploader/fileuploader.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="<?php echo base_url(JS_PUBLIC);?>/fileuploader/fileuploader.js" ></script>
<link href="<?php echo base_url(JS_PUBLIC);?>/jqueryui/jquery-ui-1.8.23.custom.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="<?php echo base_url(JS_PUBLIC);?>/jqueryui/jquery-ui-1.8.23.custom.min.js" ></script>
<script>
    $(document).ready(function(){
    	$("#frm").validate({
            rules: {
                "title": {
                    required: true
                },
                "website": {
                    required: true
                },
                "comment": {
                    required: true
                },
                "position": {
                    digits: true
                }
            },
            messages: {
            },
            errorElement: "div"
        });

        $('.datepicker').datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'dd/mm/yy'
        });
        new qq.FileUploader({
            element: document.getElementById('upload_images'),
            allowedExtensions: ['jpeg', 'jpg', 'png', 'bmp', 'gif'],
            action: '<?php echo base_url('admin/upload').'?type=partner&id='.$data['partner_id'];?>',
            debug: false,
            uploadButtonText: "Click để tải ảnh lên",
            onSubmit: function() {
                $('#loading').show();
            },
            onComplete: function(id, fileName, responseJSON){
                if (responseJSON.success) {
                    image_str = $('#images_str').val();
                    $('#upload_list').append('<img onerror="ImgError(this);" width="120" height="100" _gallery_id="'+responseJSON.id+'" src="'+BASE_URL +'/'+responseJSON.path+'"/>');
                } else {
                    alert(responseJSON.msg);
                }
                $('#loading').hide();
            }
        });
        $('#upload_images .qq-upload-button').css({width: '160px', padding: '0px 5px'});
        var uploader_single = new qq.FileUploader({
            element: document.getElementById('avatar_uploader'),
            allowedExtensions: ['jpeg', 'jpg', 'png', 'bmp', 'gif'],
            action: '<?php echo base_url('admin/upload').'?type=partner&id='.$data['partner_id'].'&mode=simple';?>',
            debug: false,
            uploadButtonText: "Thay đổi ảnh đại diện",
            multiple: false,
            onSubmit: function() {
                $('#loading').show();
            },
            onComplete: function(id, fileName, responseJSON){
                console.log(responseJSON);
                if (responseJSON.success) {
                    $('#avatar_uploader_img').attr('src', BASE_URL+responseJSON.url);
                } else {
                    alert(responseJSON.msg);
                }
                $('#loading').hide();
            },
            uploadButtonText: "Thay đổi ảnh đại diện",
        });
        $('#avatar_uploader .qq-upload-button').css({width: '160px', padding: '5px'});
        $('.qq-upload-list').remove();
        $('#upload_list img').live('dblclick', function(){
            if (!confirm('Bạn có chắc chắn muốn xóa ảnh này không ?')){
                return false;
            }
            i = $(this);
            gallery_id = i.attr('_gallery_id');
            $.post('<?php echo base_url('admin/remove_image'); ?>', {gallery_id: gallery_id}, function(data){
                if (data == true){
                    i.remove();
                } else {
                    alert('Có lỗi khi xóa ảnh. Vui lòng thử lại !');
                }
            });
        });
    });
</script>
<style>
    #upload_list img {
        margin: 5px 5px 5px 0px;
        border: 1px solid #CCC;
    }
</style>
<div id="loading" style="position: fixed;top: 20%;left: 50%;text-align: center; padding: 5px 10px; background: white;border: 1px solid #CCC;font-weight: bold; display: none;">Đang tải lên. Vui lòng chờ</div>
<form method="post" class="form-horizontal" id="frm">
    <legend><?php echo isset($data->title) ? 'Sửa' : 'Thêm mới';?> đối tác</legend>
    <?php if (isset($error)):?>
        <div class="alert alert-error"><?php echo $error;?></div>
    <?php else :?>
        <?php if (isset($msg)):?><div class="alert alert-success"><?php echo $msg;?></div><?php endif;?>
    <?php endif;?>
    <div class="control-group">
        <label class="control-label" for="title">Tên đối tác</label>
        <div class="controls">
            <input type="text" class="input-xxlarge" name="title" id="title" value="<?php echo @$data['title'];?>" placeholder="Tên đối tác">
        </div>
    </div>
    
    
    <div class="control-group">
        <label class="control-label" for="avatar">Ảnh đại diện</label>
        <div class="controls">
            <img onerror="ImgError(this);" id="avatar_uploader_img" width="170" height="150" src="<?php echo base_url('uploads/partner/'.$data['partner_id'].'/'.md5($data['partner_id']).'.jpg?t='.time());?>" />
            <div id="avatar_uploader" style=""></div>
        </div>
    </div>
    
    <div class="control-group">
        <label class="control-label" for="website">Website</label>
        <div class="controls">
            <input type="text" class="input-xxlarge" name="website" id="website" value="<?php echo @$data['website'];?>" placeholder="Website ...">
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="comment">Đối tác nói về chúng tôi</label>
        <div class="controls">
            <textarea name="comment" class="input-xxlarge" rows="5"><?php echo @$data['comment'];?></textarea>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="position">Thứ tự</label>
        <div class="controls">
            <input type="text" class="input-xxlarge" name="position" id="position" value="<?php echo @$data['position'];?>" placeholder="Thứ tự hiển thị">
        </div>
    </div>
    
    <div class="control-group" style="display:none">
        <label class="control-label" for="description">Ảnh đối tác<br /><span style="color: #999; font-size: 11px;">Click đúp chuột vào ảnh để xóa ảnh.</span></label>
        <div class="controls">
            <?php if (isset($data['partner_id'])): ?>
            <div id="upload_list">
                <div style="text-align: left; line-height:30px; color: green;">
                    <div id="upload_images">
                        <noscript>
                            <p>Please enable JavaScript to use file uploader.</p>
                        </noscript>
                    </div>
                </div>
            </div>
            <?php else :?>
            Vui lòng lưu thông tin đối tác trước khi upload ảnh sản phẩm
            <?php endif;?>
        </div>
    </div>


    <div class="control-group">
        <div class="controls">
            <?php echo isset($data) ? form_hidden('partner_id', $data['partner_id']) : '';?>
            <input type="submit" class="btn btn-primary" value="<?php echo isset($data) ? 'Lưu lại' : 'Thêm mới';?>" />
            <input type="reset" class="btn" value="Hủy bỏ" />
        </div>
    </div>
</form>